# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     tech
   Description :
   Author :       Flyoung
   date：          2023/9/5
-------------------------------------------------
   Change Activity:
                   2023/9/5:
-------------------------------------------------
"""
from typing import AnyStr

from sqlalchemy import Column, Integer, String, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session

from db.common import with_session

Base = declarative_base()


class Tech(Base):
    __tablename__ = 'tech'

    tech_id = Column(Integer, primary_key=True, autoincrement=True)
    tech_name = Column(String(255), nullable=True)
    tech_score = Column(Float, nullable=True)

    def __init__(self, tech_name, tech_score):
        self.tech_name = tech_name
        self.tech_score = tech_score

    def to_dict(self):
        return {
            'tech_id': self.tech_id,
            'tech_name': self.tech_name,
            'tech_score': self.tech_score
        }


@with_session
def get_all_tech(session: Session):
    return [i.to_dict() for i in session.query(Tech).all()]


@with_session
def add_tech(session: Session, tech_name: AnyStr, tech_score=0):
    tech = Tech(tech_name, tech_score)
    session.add(tech)
    return tech
